package com.a3dweijing.wetouch2.wxapi;

import android.content.Context;
import android.widget.Toast;

import com.a3dweijing.wetouch2.Activity.BuyActivity;
import com.a3dweijing.wetouch2.Common.CallBack;
import com.a3dweijing.wetouch2.base.App;
import com.a3dweijing.wetouch2.bean.WEIXINGSign;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2018/4/9.
 */

public class WXCallback implements Callback<JsonObject>
{
    
    private Context context;
    
    private CallBack callBack;

    private String Tag;

    public WXCallback(Context context,String tag, CallBack callBack) {
        this.context = context;
        this.callBack = callBack;
        Tag = tag;
    }


    
    @Override
    public void onResponse(Call<JsonObject> call, Response<JsonObject> response)
    {
        JsonObject body = response.body();
        if (body != null)
        {
            callBack.doNext(0, null);
            WEIXINGSign weixingSign = new Gson().fromJson(body, WEIXINGSign.class);
            if (weixingSign.isFlag())
            {
                final IWXAPI msgApi = App.getWxapi();
                PayReq request = new PayReq();
                WEIXINGSign.PayParamBean payParam = weixingSign.getPayParam();
                request.appId = payParam.getAppid();
                request.partnerId = payParam.getMch_id();
                request.prepayId = payParam.getPrepay_id();
                request.packageValue = "Sign=WXPay";
                request.nonceStr = payParam.getNonce_str();
                request.timeStamp = payParam.getTimestamp() + "";
                request.sign = payParam.getSign();
                msgApi.sendReq(request);
            }
        }
        else
        {
            callBack.doNext(1, null);
        }
    }
    
    @Override
    public void onFailure(Call<JsonObject> call, Throwable t)
    {
        callBack.doNext(-1, null);
    }
}
